home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16.lha / Python-1.6 / Lib / Python1.6 / UserList.py < prev    next >
Encoding:
Python Source  |  2000-03-31  |  2.7 KB  |  70 lines

  1. """A more or less complete user-defined wrapper around list objects."""
  2.  
  3. class UserList:
  4.     def __init__(self, initlist=None):
  5.         self.data = []
  6.         if initlist is not None:
  7.             # XXX should this accept an arbitary sequence?
  8.             if type(initlist) == type(self.data):
  9.                 self.data[:] = initlist
  10.             elif isinstance(initlist, UserList):
  11.                 self.data[:] = initlist.data[:]
  12.             else:
  13.                 self.data = list(initlist)
  14.     def __repr__(self): return repr(self.data)
  15.     def __cmp__(self, other):
  16.         if isinstance(other, UserList):
  17.             return cmp(self.data, other.data)
  18.         else:
  19.             return cmp(self.data, other)
  20.     def __len__(self): return len(self.data)
  21.     def __getitem__(self, i): return self.data[i]
  22.     def __setitem__(self, i, item): self.data[i] = item
  23.     def __delitem__(self, i): del self.data[i]
  24.     def __getslice__(self, i, j):
  25.         i = max(i, 0); j = max(j, 0)
  26.         userlist = self.__class__()
  27.         userlist.data[:] = self.data[i:j]
  28.         return userlist
  29.     def __setslice__(self, i, j, other):
  30.         i = max(i, 0); j = max(j, 0)
  31.         if isinstance(other, UserList):
  32.             self.data[i:j] = other.data
  33.         elif isinstance(other, type(self.data)):
  34.             self.data[i:j] = other
  35.         else:
  36.             self.data[i:j] = list(other)
  37.     def __delslice__(self, i, j):
  38.         i = max(i, 0); j = max(j, 0)
  39.         del self.data[i:j]
  40.     def __add__(self, other):
  41.         if isinstance(other, UserList):
  42.             return self.__class__(self.data + other.data)
  43.         elif isinstance(other, type(self.data)):
  44.             return self.__class__(self.data + other)
  45.         else:
  46.             return self.__class__(self.data + list(other))
  47.     def __radd__(self, other):
  48.         if isinstance(other, UserList):
  49.             return self.__class__(other.data + self.data)
  50.         elif isinstance(other, type(self.data)):
  51.             return self.__class__(other + self.data)
  52.         else:
  53.             return self.__class__(list(other) + self.data)
  54.     def __mul__(self, n):
  55.         return self.__class__(self.data*n)
  56.     __rmul__ = __mul__
  57.     def append(self, item): self.data.append(item)
  58.     def insert(self, i, item): self.data.insert(i, item)
  59.     def pop(self, i=-1): return self.data.pop(i)
  60.     def remove(self, item): self.data.remove(item)
  61.     def count(self, item): return self.data.count(item)
  62.     def index(self, item): return self.data.index(item)
  63.     def reverse(self): self.data.reverse()
  64.     def sort(self, *args): apply(self.data.sort, args)
  65.     def extend(self, other):
  66.         if isinstance(other, UserList):
  67.             self.data.extend(other.data)
  68.         else:
  69.             self.data.extend(other)
  70.